package com.example.onlinereadingsystem.demos.web.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.onlinereadingsystem.demos.web.dto.common.BaseContext;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @Description
 * @Author ljm
 * @Date 2024/11/12
 */
@Component
public class MetaObjectHandlerConfig implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        this.strictUpdateFill(metaObject, "createBy", String.class, BaseContext.getUsername());
        this.strictUpdateFill(metaObject, "updateBy", String.class, BaseContext.getUsername());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        this.strictUpdateFill(metaObject, "updateBy", String.class, BaseContext.getUsername());
    }
}
